package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.domain.*;
import com.itheima.service.OrderService;
import com.itheima.utils.POIUtils;
import com.itheima.utils.QiniuUtils;
import com.itheima.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.*;

/**
 * 创建时间：2020-08-22-9:41 xiaotian(●'◡'●))
 * 类说明：
 * 类注解：
 */

@Controller
@RequestMapping("/xiaotian")
@ResponseBody
public class OrderController {

    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;

    /*-----------------------------------orderSetmeal----------------------------------------*/
    @RequestMapping("/setmeal/findSetMealPage")  //查询
    public PageResult findSetMealPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderService.findSetMealPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("/setmeal/findCheckGroup")  //查询检查组
    public Result findCheckGroup(@RequestBody QueryPageBean queryPageBean) {
        List<CheckGroup> checkGroupList = orderService.findCheckGroup();
        if(checkGroupList != null && checkGroupList.size() > 0){
            Result result = new Result(true, MessageConstant.SUCCESS);
            result.setData(checkGroupList);
            return result;
        }
        return new Result(false,MessageConstant.FAIL);
    }

    @RequestMapping("/setmeal/deleteSetmeal")  //删除
    public Result deleteSetmeal(Integer id){
        try {
            orderService.deleteSetmeal(id);
            return new Result(true,MessageConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.FAIL);
        }
    }

    @RequestMapping("/checkgroup/findCheckItemPage2")
    public List<CheckGroup> findCheckItemPage2() {
        return orderService.findCheckItemPage2();
    }

    @RequestMapping("setmeal/uploadImg")
    public Result uploadImg(@RequestParam("imgFile")MultipartFile imgFile){
        try{//使用UUID随机产生文件名称，防止同名文件覆盖
            String originalFilename = imgFile.getOriginalFilename();
            String uuidName = UploadUtils.getUUIDName(originalFilename);
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),uuidName);
            //图片上传成功
            Result result = new Result(true, MessageConstant.SUCCESS);
            result.setData(uuidName);
            return result;
        }catch (Exception e){}
        return new Result(false,MessageConstant.FAIL);
    }

    //新增
    @RequestMapping("/setmeal/addSetmeal")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            orderService.addSetmeal(setmeal,checkgroupIds);
        }catch (Exception e){//新增套餐失败
            return new Result(false,MessageConstant.FAIL);
        }//新增套餐成功
        return new Result(true,MessageConstant.SUCCESS);
    }



    /*-----------------------------------CheckItem----------------------------------------*/
    @RequestMapping("/checkitem/findCheckItem")  //查询
    public PageResult findCheckItemAll(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderService.findCheckItem(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


    @RequestMapping("/checkitem/delete")  //删除
    public Result checkitemDelete(Integer id){
        try {
            orderService.checkitemDelete(id);
            return new Result(true,MessageConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.FAIL);
        }
    }


    @RequestMapping("/checkitem/addCheckitem")  //新增
    public Result addCheckitem(@RequestBody OrderCheckItem orderCheckItem)
    {
        try {
            orderService.addCheckitem(orderCheckItem);
        }catch (Exception e){
            //新增失败
            return new Result(false, MessageConstant.FAIL);
        }
        //新增成功
        return new Result(true,MessageConstant.SUCCESS);
    }

    //编辑
    @RequestMapping("/checkitem/checkitemedit")
    public Result checkitemedit(@RequestBody OrderCheckItem orderCheckItem){
        try {
            orderService.checkitemedit(orderCheckItem);
        }catch (Exception e){
            return new Result(false,MessageConstant.FAIL);
        }
        return new Result(true,MessageConstant.SUCCESS);
    }


    /*-----------------------------------OrderSetting----------------------------------------*/
    //展示页面
    @RequestMapping("/ordersetting/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//参数格式为：2019-03
        try {
            List<Map> list = orderService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.SUCCESS, list);  //获取预约设置数据成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.FAIL);  //获取预约设置数据失败
        }
    }


    //预约设置
    @RequestMapping("/ordersetting/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try{
            orderService.editNumberByDate(orderSetting);
            //预约设置成功
            return new Result(true,MessageConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //预约设置失败
            return new Result(false,MessageConstant.FAIL);
        }
    }

    @RequestMapping("/ordersetting/upload")  //Excel文件上传，并解析文件内容保存到数据库
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        try {//读取Excel文件数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            if(list != null && list.size() > 0){
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]),Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);
                }
                orderService.addUpload(orderSettingList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.FAIL);
        }
        return new Result(true,MessageConstant.SUCCESS);
    }




    /*-----------------------------------order.html----------------------------------------*/
    /** 2020/8/22 11:03   xiaotian
     *查询order表
     */
    @RequestMapping("/findOrderAll")
    public List<Order> findOrderAll(){
        return orderService.findOrderAll();
    }

    //回显
    @RequestMapping("/findOrder")
    public Order findOrder(Integer id){
        return orderService.findOrder(id);
    }


    /** 2020/8/26 16:23   xiaotian
     *  条件查询
     */
/*    @RequestMapping("/findOrderByPage")
    public List<Order> findOrderByPage(@RequestBody PageParams pageParams){
        return orderService.findOrderByPage(pageParams);
    }*/


    /** 2020/8/26 16:23   xiaotian
     *  分页查询
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


    /** 2020/8/26 16:23   xiaotian
     *  新增某条order表
     */
    @RequestMapping("/orderAdd")
    public void orderAdd(@RequestBody Order order){
        orderService.orderAdd(order);
    }

    /** 2020/8/22 11:03   xiaotian
     *删除某条order表
     */
    @RequestMapping("/deleteOrder")
    public void deleteOrder(int id){
        orderService.deleteOrder(id);
    }

    /** 2020/8/26 16:23   xiaotian
     *  修改某条order表
     */
    @RequestMapping("/orderUpdate")
    public void orderUpdate(@RequestBody Order order){
        orderService.orderUpdate(order);
    }



    /*--------------------------------------------------checkgroup-------------------------------------------------------*/

    //查询所有检查项OrderCheckItem
    @RequestMapping("/checkitem/findAll")
    public Result findAll(){
        List<OrderCheckItem> checkItemList = orderService.findCheckItemAll();
        if(checkItemList != null && checkItemList.size() > 0){
            Result result = new Result(true, MessageConstant.SUCCESS);
            result.setData(checkItemList);
            return result;
        }
        return new Result(false,MessageConstant.FAIL);
    }

    //新增按钮，新增检查项和检查组，注意：在group也有的类似的
    @RequestMapping("/checkitem/addcheckitem")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds)
    {
        try {
            orderService.addcheckitem(checkGroup,checkitemIds);
        }catch (Exception e){
        //新增失败
            return new Result(false, MessageConstant.FAIL);
        }
        //新增成功
        return new Result(true,MessageConstant.SUCCESS);
    }


    /** 2020/8/26 16:23   xiaotian
     *  分页条件查询
     */
    @RequestMapping("/checkgroup/findCheckItemPage")
    public PageResult findCheckItemPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderService.findCheckItemPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


    //修改：根据id查询回显当前行数据
    @RequestMapping("/checkgroup/findById")
    public Result findById(Integer id){
        CheckGroup checkGroup = orderService.findById(id);
        if(checkGroup != null){
            Result result = new Result(true, MessageConstant.SUCCESS);
            result.setData(checkGroup);
            return result;
        }
        return new Result(false,MessageConstant.FAIL);
    }


    //根据检查组合id查询对应的所有检查项id
    @RequestMapping("/checkgroup/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try{
            List<Integer> checkitemIds =
                    orderService.findCheckItemIdsByCheckGroupId(id);
            return new
                    Result(true,MessageConstant.SUCCESS,checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.FAIL);
        }
    }

    //编辑
    @RequestMapping("/checkgroup/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            orderService.edit(checkGroup,checkitemIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.FAIL);
        }
        return new Result(true,MessageConstant.SUCCESS);
    }

    //删除CheckGroup的一条数据
    @RequestMapping("/deleteCheckGroup")
    public void deleteCheckGroup(int id){
        orderService.deleteCheckGroup(id);

    }

}
